package altanovela.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name	 = MainController.loginPage,
			location = "/pages/login-page.ftl",
			type	 = "freemarker"),
	@Result(name	 = MainController.homePage,
			location = "/pages/home-page.ftl",
			type	 = "freemarker")	
})
@ParentPackage("default")
public class MainController extends ActionSupport{	
	public final static String loginPage = "loginPage";
	public final static String homePage  = "homePage";
	
	/**
	 * nilainya akan di kirimkan melalui login-page.ftl
	 */
	public static String hisname;
	public Map session = ActionContext.getContext().getSession();
	
	@Action(value = "login", 
			interceptorRefs={@InterceptorRef("myInterceptStack")})
	public String doLoginPage(){
		return loginPage;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value = "doLogin", 
			interceptorRefs={@InterceptorRef("myInterceptStack")})
	public String doLoginProcess(){
		if(hisname == null){
			return loginPage;
		}
		session.put("login", hisname);
		return homePage;
	}
	
	@Action(value="logout")
	public String doLogout(){
		session.remove("login");
		setHisname(null);
		return loginPage;
	}
	
	// COMMON USED METHOD
	public static void setName(String name){
		hisname = name;
	}
	
	// SETTER AND GETTER
	public String getHisname() {
		return hisname;
	}
	public void setHisname(String hisname) {
		this.hisname = hisname;
	}
}

